package com.zhongkai.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
@Component
public class BaseDao extends HibernateDaoSupport {
	@Resource(name="sessionFactory")
	public void setBaseDaoSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	public void delete(Object entity) throws Exception {
		this.getHibernateTemplate().delete(entity);
	}
	public void insert(Object entity) throws Exception {
		this.getHibernateTemplate().save(entity);
	}
	public void update(Object entity) throws Exception {
		this.getHibernateTemplate().update(entity);
	}

	public void insertOrUpdate(Object entity) throws Exception {
		this.getHibernateTemplate().saveOrUpdate(entity);
	}

	public Object selectById(String entityName, Serializable id)
			throws Exception {
		// return this.getHibernateTemplate().load(entityName, id);参考方法
		return this.getHibernateTemplate().get(entityName, id);
	}

	public Object selectById(Class entityClass, Serializable id)
			throws Exception {
		// return this.getHibernateTemplate().load(entityName, id);参考方法
		return this.getHibernateTemplate().get(entityClass, id);
	}

	public List select(String queryString) throws Exception {
		return this.getHibernateTemplate().find(queryString);
	}
	public List select(String queryString, Object value) throws Exception {
		return this.getHibernateTemplate().find(queryString, value);
	}
	public List select(String queryString, Object[] values) throws Exception {
		return this.getHibernateTemplate().find(queryString, values);
	}
	public List select(final String queryString,final Integer pageIndex,final Integer pageMax) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {			
				Query query = s.createQuery(queryString);
				query.setFirstResult((pageIndex.intValue() - 1)
						* pageMax.intValue());
				query.setMaxResults(pageMax);
				List results = query.list();
				return results;
			}
		});
	}
	
	public List selectBlur(final String queryString) {//模糊查询结果
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {			
				Query query = s.createQuery(queryString);
				List results = query.list();
				return results;
			}
		});
	}
	
	public Integer count(String queryString) throws Exception {
		int count;
		count = this.getHibernateTemplate().find(queryString).size();
		return count;
	}
	public Integer count(String tablename,String where) throws Exception {		
		StringBuffer hql = new StringBuffer("select count(*) from ").append(tablename).append(" ").append(where);
		Integer count = (Integer)getHibernateTemplate().find(hql.toString()).listIterator().next();
		return count.intValue();		
	}
	public Integer count2(String tablename,String where) throws Exception {
		StringBuffer hql = new StringBuffer("select count(*) from ").append(tablename).append(" ").append(where);
		return getHibernateTemplate().find(hql.toString()).size();
	}
	public void exec(String hql, Object[] values) throws Exception {
		this.getHibernateTemplate().bulkUpdate(hql, values);
	}
	public void del(String hql) throws Exception {
		Session session = this.getSession();
		session.delete(hql);
	}
	public Object findBySql(final String sql) throws Exception {
		Object object=getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				SQLQuery query=session.createSQLQuery(sql);
				List list=query.list();
				if(list!=null && list.size()>0)
					return list.get(0);
				return null;
			}
		});
		return object;
	}
	public void executeHQL(final String hql, final Object[] values) throws Exception{
		this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				for(int i=0;i<values.length;i++){
					query.setParameter(i, values[i]);
				}
				query.executeUpdate();
				return null;
			}
		});
	}
	
	/**
	 * 缓存查询
	 * **/
	public List selectForCache(final String queryString) throws Exception {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {			
				Query query = s.createQuery(queryString);
				List results = query.setCacheable(true).list();
				return results;
			}
		});
	}
	
	/**
	 * 缓存查询(用于分页)
	 * **/
	public List selectForCache(final String queryString,final Integer pageIndex,final Integer pageMax) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {			
				Query query = s.createQuery(queryString);
				query.setFirstResult((pageIndex.intValue() - 1)
						* pageMax.intValue());
				query.setMaxResults(pageMax);
				List results = query.setCacheable(true).list();
				return results;
			}
		});
	}
}
